package com.jfsnpm.common;


import java.util.Properties;

import org.snaker.jfinal.plugin.SnakerPlugin;

import cn.dreampie.sqlinxml.SqlInXmlPlugin;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;
import com.jfinal.plugin.activerecord.tx.TxByRegex;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.render.ViewType;
import com.jfsnpm.authority.Auth;
import com.jfsnpm.controller.IndexController;
import com.jfsnpm.plugin.jfinal.autoroutes.AutoBindRoutes;
import com.jfsnpm.plugin.snakerflow.QuartzPlugin;
import com.jfsnpm.plugin.viewoffice.ViewOfficeHandler;

import freemarker.template.TemplateModelException;



public class AppConfig extends JFinalConfig {
	public static boolean DevMode=false;
	public static Properties properties;
	public static C3p0Plugin c3p0Plugin;
	public void configConstant(Constants me) {
		// 获取配置文件getProperty(...)
		properties = loadPropertyFile("config.txt");
		DevMode=getPropertyToBoolean("devMode", false);
		me.setDevMode(DevMode);
		me.setViewType(ViewType.FREE_MARKER);
		me.setBaseViewPath("WEB-INF/pm/");
		me.setFileRenderPath(properties.getProperty("filepath","/jfsnpmFileSystem/"));
		me.setUploadedFileSaveDirectory(properties.getProperty("filepath","/jfsnpmFileSystem/"));
		//me.setError404View("WEB-INF/pm/common/404.html");
	}
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add(new AutoBindRoutes());
		me.add("/", IndexController.class);
		//me.add("/ViewOffice", ViewOfficeController.class);
	}
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 本系统数据库插件
		String dabaseUse = getProperty("databaseUse","mysql");
		c3p0Plugin = new C3p0Plugin(getProperty(dabaseUse+"_jdbcUrl"),
				getProperty(dabaseUse+"_user"), getProperty(dabaseUse+"_password").trim(),
				getProperty(dabaseUse+"_jdbcClass"));
		me.add(c3p0Plugin);
		// ActiveRecord
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		if(DevMode) arp.setShowSql(true);
		if("mssql".equals(dabaseUse)) arp.setDialect(new AnsiSqlDialect());
		// arp.addMapping("oa_user", User.class);
		
		//sql语句plugin
		me.add(new SqlInXmlPlugin(dabaseUse));
		
		// SnakerFlow插件
		SnakerPlugin snakerPlugin = new SnakerPlugin(c3p0Plugin,properties);
	    me.add(snakerPlugin);
	    // 调用SQL初始化数据库
	 	//ScriptsPlugin scriptsPlugin = new ScriptsPlugin(c3p0Plugin);
	 	//me.add(scriptsPlugin);
	    // Quartz调度插件
	    QuartzPlugin quartzPlugin = new QuartzPlugin();
	    me.add(quartzPlugin);
	}
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new TxByRegex(".+_save\\b"));//以"_save"结尾的方法启用事务
		me.add(new GlobalInterceptor());//全局拦截器 1.验证登陆 2.记录历史
	}
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new ViewOfficeHandler());
	}
	/**
	 * 配置模板全局变量
	 */
	public void afterJFinalStart(){
		try {
			FreeMarkerRender.getConfiguration().setClassicCompatible(true);
			//FreeMarkerRender.getConfiguration().setDateFormat("yyyy/mm/dd");
			FreeMarkerRender.getConfiguration().setSharedVariable("rootpath", JFinal.me().getContextPath());
			FreeMarkerRender.getConfiguration().setSharedVariable("auth", new Auth());
		} catch (TemplateModelException e) {
			throw new RuntimeException(e);
		}
		
	  }
}
